import thread
import weakref
import time
import gobject


class Timer:

    def alarmIn(self, delay, instance, action, *args):
        gobject.timeout_add(int(delay*1000), self.delayed_response, delay,
                            weakref.ref(instance), action, args)
        #thread.start_new(self.delayed_response,
        #                 (delay, weakref.ref(instance), action, args))

    def delayed_response(self, delay, winstance, action, args):
        #time.sleep(delay)
        instance = winstance()
        if instance is not None:
            try:
                action(instance, *args)
            except:
                import traceback
                print "Oh! Error in Timer thread"
                traceback.print_exc()


timer = Timer()
